package com.cskaoyan.service.system;

import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketAdmin;
import com.cskaoyan.bean.po.MarketAdminExample;
import com.cskaoyan.bean.po.MarketStorage;
import com.cskaoyan.mapper.MarketStorageMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ObjectStorageServiceImpl implements ObjectStorageService{

    @Autowired
    MarketStorageMapper mapper;

    @Override
    @Transactional
    public MarketStorage createFile(String hexString, String originalFilename, String contentType, long size, String url) {
        MarketStorage marketStorage = new MarketStorage();
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(d);
        marketStorage.setKey(hexString);
        marketStorage.setName(originalFilename);
        marketStorage.setType(contentType);
        marketStorage.setSize((int) size);
        marketStorage.setUrl(url);
        marketStorage.setUpdateTime(d);
        marketStorage.setAddTime(d);
        mapper.insert(marketStorage);
        return marketStorage;
    }

    @Override
    public CommonData<MarketStorage> selectStorage(Integer page, Integer limit, String sort, String order, String name, String key) {
        PageHelper.startPage(page, limit);
        List<MarketStorage> marketAdmins = mapper.selectStorage(name,key);
        CommonData<MarketStorage> marketAdminCommonData = new CommonData<>();
        PageInfo<MarketStorage> pageInfo = new PageInfo<>(marketAdmins);
        marketAdminCommonData.setLimit(limit);
        marketAdminCommonData.setPage(page);
        marketAdminCommonData.setTotal((int) pageInfo.getTotal());
        marketAdminCommonData.setPages(pageInfo.getPages());
        marketAdminCommonData.setList(pageInfo.getList());
        return marketAdminCommonData;    }

    @Override
    @Transactional
    public MarketStorage updateStorage(MarketStorage marketStorage) {
        int i = mapper.updateByPrimaryKey(marketStorage);
        return marketStorage;
    }

    @Override
    @Transactional
    public MarketStorage deleteStorage(MarketStorage marketStorage) {
        MarketStorage storage = mapper.selectByPrimaryKey(marketStorage.getId());
        int i = mapper.deleteByPrimaryKey(marketStorage.getId());
        return storage;
    }
}
